var prevMenu;
$(document).ready(function(event){
	var menu = $(".header-menu .menu ul").children("li").not(".menu-space");
	init($(menu));
	
	$(menu).each(function(){
		$(this).mouseenter(function(){
			if($(this).hasClass("menu-on")) return;
			$(this).removeClass().addClass("menu-on-temp");	
		}).mouseleave(function(){
			if($(this).hasClass("menu-on")) return;
			$(this).removeClass().addClass("menu-off");	
		});
		
		$(this).on("click", function(){
			if($(this).hasClass("menu-on")) return;
			clickFunc($(this));
		}); 
	});
	
	$(":input[name='btnUserRevise']").each(function(idx, item){
		$(item).on("click", function(event){
			var usrInfo = $(item).parent("li").parent("ul").find(">li").eq(0).children("span");
			var usrId = $(usrInfo).eq(1).text();
			var usrNm = $(usrInfo).eq(0).text();
			var options = new Object();
			options["url"] = "/template/system/userInfo/_regFormRevise/"+usrId+"?isMe=true";
			options["jqwId"] = "reviseJqwPop";
			options["title"] = "["+usrNm+"] 사용자 수정";
			options["titleFontColor"] = "";
			options["resizable"] = false;
			options["width"] = "500";
			options["height"] = "330";
			jqwOpen(options);
		});
	});
	
	$("#goToLoginBtn").on("click", function(event){
		var options = new Object();
		options["url"] = "/template/auth/login";
		options["type"] = "POST";
		options["dataType"] = "html";
		options["obj"] = $(menu);
		loadMenu(options);
	});
});

function init(menu){
	prevMenu = $(menu).eq(0);
}

function clickFunc(menu){
	var url = "";
	var obj = $(menu).children("span");	
	
	if($(obj).length > 0 && $(obj).eq(0).length > 0) url = $(obj).eq(0).text();
	if(url == "") {
		alert("Not ready yet!"); 
		return;
	}
	
	selectedMenu($(menu));
	
	var options = new Object();
	options["url"] = url;
	options["type"] = "POST";
	options["dataType"] = "html";
	options["obj"] = $(menu);
	loadMenu(options);
}

function selectedMenu(obj){
	$(obj).siblings().not(".menu-space").removeClass().addClass("menu-off");
	$(obj).removeClass().addClass("menu-on");
};

function loadMenu(opts) {//path, mType, dtype, obj) {
	var ajax = new jqMyAjax();
	var obj = opts["obj"];
	var chkObj = ($(obj) != null && $(obj) != "undefined")?true : false;
	var data = (chkObj)?"sysCode="+$(obj).children("span").eq(1).text():"";
	var option = new Array();
	option["type"] = opts["type"];
	option["url"] = opts["url"];
	option["dataType"] = opts["dataType"];
	option["data"] = data;
	option["success"] = function(result){
		prevMenu = (chkObj)?$(obj):null;
		$(".content").html(result);
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
		
		selectedMenu($(prevMenu));
	};
	
	ajax.callAjax(option);
};

function jqwOpen(opts){	
	var options = new Object();
	options["url"] = opts["url"];
	options["type"] = "POST";
	options["dataType"] = "html";
	options["jqwId"] = opts["jqwId"];
	options["jqwType"] = "popup";	//< "popup" | "save" | "close" ] >
	options["title"] = opts["title"];
	options["titleFontColor"] = opts["titleFontColor"];
	options["resizable"] = opts["resizable"];
	options["width"] = opts["width"];
	options["height"] = opts["height"];
	
	var jqw = new jqWindow();
	jqw.intializer(); //Close all jQuery.Window
	jqw.jqwSet(options);
	jqw.run();
};